package com.biostime.qdingding.modules.qd.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.biostime.qdingding.common.config.Global;
import com.biostime.qdingding.common.persistence.Page;
import com.biostime.qdingding.common.utils.StringUtils;
import com.biostime.qdingding.common.web.BaseController;
import com.biostime.qdingding.modules.qd.entity.Coupon;
import com.biostime.qdingding.modules.qd.entity.MemberCoupon;
import com.biostime.qdingding.modules.qd.service.CouponService;
import com.biostime.qdingding.modules.sys.utils.UserUtils;

/**
 * 优惠券Controller
 * @author king
 * @version 2016-02-17
 */
@Controller
@RequestMapping(value = "${adminPath}/qd/coupon")
public class CouponController extends BaseController {

	@Autowired
	private CouponService couponService;
	
	@ModelAttribute
	public Coupon get(@RequestParam(required=false) String id) {
		Coupon entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = couponService.get(id);
		}
		if (entity == null){
			entity = new Coupon();
		}
		return entity;
	}
	
	@RequestMapping(value="grant")
	public String grant(@RequestParam(required=false) String id ,Model model ){
		MemberCoupon memberCoupon =  new MemberCoupon();
		memberCoupon.setCouponId(Integer.valueOf(id));
		model.addAttribute("memberCoupon",memberCoupon);
		return "modules/qd/grantDialog";
	}
	
	@RequiresPermissions("qd:coupon:view")
	@RequestMapping(value = {"list", ""})
	public String list(Coupon coupon, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Coupon> page = couponService.findPage(new Page<Coupon>(request, response), coupon); 
		model.addAttribute("page", page);
		return "modules/qd/couponList";
	}

	@RequiresPermissions("qd:coupon:view")
	@RequestMapping(value = "form")
	public String form(Coupon coupon, Model model) {
		couponService.determination(coupon);
		model.addAttribute("coupon", coupon);
		return "modules/qd/couponForm";
	}

	@RequiresPermissions(value={"qd:coupon:edit","qd:coupon:add"})
	@RequestMapping(value = "save")
	public String save(HttpServletRequest request, Coupon coupon, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, coupon)){
			return form(coupon, model);
		}
		String[] useRanges = request.getParameterValues("useRanges");
		StringBuffer buffer = new StringBuffer("|");
		if(useRanges != null && useRanges.length > 0){
			for (int i = 0; i < useRanges.length; i++) {
				buffer.append(useRanges[i]+"|");
			}
		}
		coupon.setUseRange(buffer.toString());
		coupon.setCenterId(UserUtils.getCenter().getId());
		couponService.save(coupon);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.getAdminPath()+"/qd/coupon/?repage";
	}
	
	@RequiresPermissions("qd:coupon:delete")
	@RequestMapping(value = "delete")
	public String delete(@RequestParam(value="ids",required=false) String idsRequest, Coupon coupon, RedirectAttributes redirectAttributes) {
		//批量删除
		if(!StringUtils.isEmpty(idsRequest)){
			String[] ids = idsRequest.split(",");
			for(String id : ids){
				couponService.delete(new Coupon(id));
			}
		}else{
			couponService.delete(coupon);
		}
		
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.getAdminPath()+"/qd/coupon/?repage";
	}

}